//
//  RGDataBase.m
//  RGPopoverTagView
//
//  Created by Rain on 2018/8/22.
//  Copyright © 2018 - 2019 Rain. All rights reserved.
//

#import "RGDataBase.h"

@implementation RGDataBase

- (instancetype)init {
    if (self = [super init]) {
        self.database = [FMDatabase databaseWithPath:[RGDataBase getDataBasePath]];
        if (![self.database open]) {
            return nil;
        }
    }
    return self;
}

+ (RGDataBase *)sharedDataBase {
    static RGDataBase *database = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        database = [[RGDataBase alloc] init];
    });
    return database;
}

+ (NSString *)getDataBasePath {
    NSString *docpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *fileName = [docpath stringByAppendingPathComponent:@"GWData.sqlite"];
    return fileName;
}

// 获取所有搜索记录
- (NSMutableArray *)getAllSeachTextFromTable:(NSString *)tableName {
    NSString *sql = [NSString stringWithFormat:@"select * from %@ order by ID desc", tableName];
    //保存所有数据的数组
    NSMutableArray *seachTexts = [NSMutableArray array];
    FMResultSet *results = [self.database executeQuery:sql];
    NSInteger count = 0;
    while (results.next) {
        NSString *result = [results stringForColumn:@"NAME"];
        [seachTexts addObject:result];
        count++;
    }
    return seachTexts;
}

- (FMResultSet *)resultSetFromTable:(NSString *)tableName
                          searchKey:(NSString *)searchKey
                        searchValue:(NSString *)searchValue
{
    NSString *sql = [NSString stringWithFormat:@"select * from %@ where %@ = ?", tableName, searchKey];
    return [self.database executeQuery:sql,searchValue];
}

- (FMResultSet *)resultSetBySearchKey:(NSString *)searchKey
                          searchValue:(NSString *)searchValue
{
    return [self resultSetFromTable:@"SSCODE" searchKey:searchKey searchValue:searchValue];
}

- (FMResultSet *)resultSetByTypeID:(NSString *)typeID {
    return [self resultSetBySearchKey:@"TYPEID" searchValue:typeID];
}

- (NSMutableArray *)getSearchTextsFromTable:(NSString *)tableName
                                  searchKey:(NSString *)searchKey
                                searchValue:(NSString *)searchValue
                                  resultKey:(NSString *)resultKey
{
    FMResultSet *results = [self resultSetFromTable:tableName
                                          searchKey:searchKey
                                        searchValue:searchValue];
    NSMutableArray *searchTexts = [NSMutableArray array];
    NSInteger count = 0;
    while (results.next) {
        NSString *result = [results stringForColumn:resultKey];
        [searchTexts addObject:result];
        count++;
    }
    NSLog(@"%@", @(count));
    return searchTexts;
}


- (NSMutableArray *)getSearchObjectsFromTable:(NSString *)tableName
                                  searchKey:(NSString *)searchKey
                                searchValue:(NSString *)searchValue
                                  resultKey:(NSString *)resultKey
{
    FMResultSet *results = [self resultSetFromTable:tableName
                                          searchKey:searchKey
                                        searchValue:searchValue];
    NSMutableArray *searchObjects = [NSMutableArray array];
    NSInteger count = 0;
    while (results.next) {
        id result = [results objectForColumn:resultKey];
        id resultCode = [results objectForColumn:@"CODE"];
        [searchObjects addObject:@{@"name":result,
                                   @"code":resultCode
                                   }];
        count++;
    }
    NSLog(@"%@", @(count));
    return searchObjects;
}

- (NSMutableArray *)getSearchTextsByTypeID:(NSString *)typeID
                                 resultKey:(NSString *)resultKey
{
    return [self getSearchTextsFromTable:@"SSCODE"
                               searchKey:@"TYPEID"
                             searchValue:typeID
                               resultKey:resultKey];
}
- (NSMutableArray *)getSearchObjectsByTypeID:(NSString *)typeID
                                   resultKey:(NSString *)resultKey{
    return [self getSearchObjectsFromTable:@"SSCODE"
                               searchKey:@"TYPEID"
                             searchValue:typeID
                               resultKey:resultKey];
}
- (NSMutableArray *)getSearchTextsByTypeID:(NSString *)typeID {
    return [self getSearchTextsByTypeID:typeID
                              resultKey:@"NAME"];
    
}
- (NSMutableArray *)getSearchObjectsByTypeID:(NSString *)typeID{
    return [self getSearchObjectsByTypeID:typeID
                              resultKey:@"NAME"];
}
@end
